package theone;

import java.util.ArrayList;

import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import theone.ui.UiBoard;
import theone.ui.UiButton;
import theone.ui.UiCheckbox;
import theone.ui.UiComponent;
import theone.ui.UiCompositeComponent;
import theone.ui.UiLabel;
import theone.ui.UiPanel;

public class TestCursor extends BasicGame{
	
	private static final String COPYRIGHT = "Copyright 2013 (c) - The BLITZ Team";

	private static final String TITLE = "The ONE";

	boolean exitMe = false;
	
	private Image paper_background;
	private Image wood_background;
    private Image cursor_img;

	private Font font_fruktur;

	private Font font_fruktur_128;
	
	private ArrayList<UiComponent> components = new ArrayList<UiComponent>();

	private Image rpg_ui;

	private boolean debug = false;

	private float mouse_x;

	private float mouse_y;

	private Game game;
	
	private UiCompositeComponent components_game = new UiCompositeComponent();

	private Image marble_img;

	private Image pieces_img;


	public TestCursor(Game game) {
		super("TEST CURSOR");
		
		this.game = game;
	}

	/*public TestCursor() {
		super("TEST CURSOR");
	}*/

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Game game = new Game();
		for (int i = 0; i < 12; i++)
			for (int j = 0; j < 12; j++) {
				game.getCells()[i][j] = new Cell();
				game.getCells()[i][j].state = CellState.VOID;
			}
		
		for (int i = 2; i < 10; i++)
			for (int j = 2; j < 10; j++) {
				game.getCells()[i][j].state = CellState.EMPTY;
			}
		
		game.getCells()[5][5].state = CellState.WHITE;
		game.getCells()[6][5].state = CellState.WHITE;
		game.getCells()[7][5].state = CellState.WHITE;
		game.getCells()[5][6].state = CellState.BLACK;
		game.getCells()[6][6].state = CellState.WHITE;
		game.getCells()[7][6].state = CellState.BLACK;
		
		game.state = GameState.MAIN_MENU;
		
		try {
			TestCursor win1 = new TestCursor(game);
			AppGameContainer container = new AppGameContainer(win1);
			container.setDisplayMode(800,600,false);
			container.setMouseGrabbed(true);
			container.setShowFPS(false);
			container.start();
		} catch (SlickException e) {
		        e.printStackTrace();
		}
	}

	/*private void show() {
		boolean sharedContextTest = false;
		exitMe = false;
        
        try {
			if (sharedContextTest) {
			        GameContainer.enableSharedContext();
			        exitMe = true;
			}
			
			AppGameContainer container = new AppGameContainer(this);
			container.setForceExit(!sharedContextTest);
			container.setDisplayMode(800,600,false);
			container.setMouseGrabbed(true);
			container.setShowFPS(false);
			container.start();
			//container.se
			
			if (sharedContextTest) {
			        System.out.println("Exit first instance");
			        exitMe = false;
			        container = new AppGameContainer(this);
			        container.setDisplayMode(800,600,false);
			        container.start();
			}
        } catch (SlickException e) {
                e.printStackTrace();
        }
	}*/

	@Override
	public void render(GameContainer arg0, Graphics g) throws SlickException {
		
		
		if (this.game.state == GameState.MAIN_MENU)
		{
			renderMainMenu(arg0, g);
		}
		else
		{
			renderGame(arg0, g);
		}
		
		
		
		// Draw cursor
		g.drawImage(cursor_img, mouse_x, mouse_y);
	}

	private void renderGame(GameContainer arg0, Graphics g) {
		
		// Draw components
		components_game.draw(g, 0, 0);

	}

	private void renderMainMenu(GameContainer arg0, Graphics g) {
		// Draw components
		for (UiComponent comp : components)
			comp.draw(g, 0, 0);

		// Debug Components
		if (debug) {
			g.setColor(new Color(255, 0, 0, 255));
			for (UiComponent comp : components)
				g.drawRect(comp.getX(), comp.getY(), comp.getWidth(), comp.getHeight());
		}
	}

	@Override
	public void init(GameContainer arg0) throws SlickException {

		// Load cursor image
		cursor_img = new Image("gfx/rpg_mouse.png");
		
		// Load 'paper' background
		paper_background = new Image("gfx/paper_background.png");
		// Load 'wood' background
		wood_background = new Image("gfx/wood_background.png");
		
		// Load RPG UI
		rpg_ui = new Image("gfx/RPG_GUI_v1.png");
		
		// Load marble for board
		marble_img = new Image("gfx/marble.png");
		
		// Load pieces for board
		pieces_img = new Image("gfx/chess-pieces_0.png");
				
		// Load default font
		font_fruktur = new AngelCodeFont("fonts/fruktur.fnt", "fonts/fruktur_0.png");
		//font_fruktur_128 = new BmFont("fonts/fruktur_128.fnt", "fonts/");
		font_fruktur_128 = new AngelCodeFont("fonts/fruktur_128.fnt", "fonts/fruktur_128_0.png");
		
		
		// Build border 1
		Image[] border1_images = new Image[8];
		border1_images[0] = rpg_ui.getSubImage(860, 393, 24, 24); // OK
		border1_images[1] = rpg_ui.getSubImage(860, 498, 24, 24); // OK
		border1_images[2] = rpg_ui.getSubImage(982, 498, 24, 24); // OK
		border1_images[3] = rpg_ui.getSubImage(982, 393, 24, 24); // OK
		
		border1_images[4] = rpg_ui.getSubImage(902, 393, 70, 24); // OK
		border1_images[5] = rpg_ui.getSubImage(860, 428, 24, 50); // OK
		border1_images[6] = rpg_ui.getSubImage(902, 498, 70, 24); // OK
		border1_images[7] = rpg_ui.getSubImage(982, 428, 24, 50);
		
		// Load background as Panel
		UiPanel bck = new UiPanel(wood_background, border1_images);
		bck.setX(0);
		bck.setY(0);
		bck.setWidth(arg0.getWidth());
		bck.setHeight(arg0.getHeight());
		components.add(bck);
		
		// Load a label
		UiLabel label_title = new UiLabel(font_fruktur_128, TITLE);
		label_title.setX(0);
		label_title.setY(0);
		label_title.setWidth(arg0.getWidth());
		label_title.setHeight(arg0.getHeight()/3);
		components.add(label_title);
		
		// Load Panel for menu
		UiPanel bck_menu = new UiPanel(paper_background, border1_images);
		bck_menu.setX(arg0.getWidth()/4);
		bck_menu.setY(arg0.getHeight()/3);
		bck_menu.setWidth(arg0.getWidth()/2);
		bck_menu.setHeight(arg0.getHeight()*3/6);
		components.add(bck_menu);
		
		
		// Load a button
		UiButton button = new UiButton(wood_background, border1_images, font_fruktur, "New Game");
		button.setX(bck_menu.getX() + bck_menu.getWidth()/4);
		button.setY(bck_menu.getY()+25);
		button.setWidth(bck_menu.getWidth()/2);
		button.setHeight(50);
		components.add(button);
		
		UiPanel pan33 = new UiPanel(wood_background, border1_images);
		pan33.setX(bck_menu.getX() + bck_menu.getWidth()/4);
		pan33.setY(bck_menu.getY()+25+100);
		pan33.setWidth(bck_menu.getWidth()/2);
		pan33.setHeight(50);
		pan33.setHoverSensitive(true);
		components.add(pan33);
		
		
		/*// Load a Checkbox
		UiCheckbox checkbox = new UiCheckbox(false, rpg_ui.getSubImage(150, 15, 20, 20), 
				rpg_ui.getSubImage(150, 15, 20, 20), font_fruktur);
		checkbox.setX(400);
		checkbox.setY(50);
		//components.add(checkbox);
		
		// Load a label
		UiLabel label = new UiLabel(font_fruktur, "Test label");
		label.setX(50);
		label.setY(150);
		//components.add(label);
		
		// Load a panel
		UiPanel panel = new UiPanel(wood_background, border1_images);
		panel.setX(50);
		panel.setY(250);
		panel.setWidth(268);
		panel.setHeight(268);
		//components.add(panel);*/
		
		
		// Add copyright
		UiLabel label_copyright = new UiLabel(font_fruktur, COPYRIGHT);
		label_copyright.setX(0);
		label_copyright.setY(arg0.getHeight()*5/6);
		label_copyright.setWidth(arg0.getWidth());
		label_copyright.setHeight(arg0.getHeight()/6);
		components.add(label_copyright);
		
		
		
		// Init board
		UiPanel bck_board = new UiPanel(wood_background, border1_images);
		bck_board.setX(0);
		bck_board.setY(0);
		bck_board.setWidth(arg0.getWidth());
		bck_board.setHeight(arg0.getHeight());
		components_game.add(bck_board);
		
		UiPanel bck_board_in = new UiPanel(paper_background, border1_images);
		bck_board_in.setX(25);
		bck_board_in.setY(25);
		bck_board_in.setWidth(arg0.getWidth()-50);
		bck_board_in.setHeight(arg0.getHeight()-50);
		components_game.add(bck_board_in);
		
		Image marble_on = marble_img.getSubImage(0, 0, 50, 50);
		Image marble_off = marble_img.getSubImage(0, 0, 50, 50);
		Image piece_white = pieces_img.getSubImage(0, 0, 100, 100).getScaledCopy(2);
		Image piece_black = pieces_img.getSubImage(0, 0, 100, 100).getScaledCopy(2);;
		
		UiBoard board = new UiBoard(marble_on, marble_off, piece_white, piece_black, this.game);
		board.setX(30);
		board.setY(30);
		board.setWidth(arg0.getWidth()-60);
		board.setHeight(arg0.getHeight()-60);
		components_game.add(board);
		
		
	}

	@Override
	public void update(GameContainer gc, int arg1) throws SlickException {
		
		// Update over of components
		for (UiComponent comp : components) {
			comp.setHover(false);
			if (mouse_x > comp.getX() && mouse_x < comp.getX() + comp.getWidth() &&
					mouse_y > comp.getY() && mouse_y < comp.getY() + comp.getHeight()) {
				comp.setHover(true);
			}
		}


		
		if (exitMe)
			gc.exit();
		
	}
	
	public void mouseMoved(int oldx,
            int oldy,
            int newx,
            int newy)
	{
		mouse_x += newx;
		mouse_y += newy;
	}

	@Override
	public void keyPressed(int key, char c) {
		
		// If key then exit
        if (key == Input.KEY_SPACE || key == Input.KEY_ESCAPE) {
        	exitMe = true;
        }
        if (key == Input.KEY_F1) {
        	debug  = (!debug);
        }
        
        if (key == Input.KEY_F2) {
        	this.game.state  = GameState.MAIN_MENU;
        }
        if (key == Input.KEY_F3) {
        	this.game.state  = GameState.GAME;
        }
        
	}
}
